package service.impl;

import java.util.List;

import pojo.MerchantApplication;
import service.MerchantApplicationManager;
import dao.MerchantApplicationDao;
import dao.factory.DaoFactory;
import data.VersionTable;

public class MerchantApplicationManagerImpl implements MerchantApplicationManager{
	
	private MerchantApplicationDao merchantApplicationDao;
	private VersionTable versionTable;
	
	public MerchantApplicationManagerImpl(){
		merchantApplicationDao = DaoFactory.getMerchantApplicationDaoInstance();
		versionTable = VersionTable.getInstance();
	}

	@Override
	public int add(MerchantApplication merchantApplication) {
		return merchantApplicationDao.add(merchantApplication);
	}

	@Override
	public List<MerchantApplication> getAll() {
		return merchantApplicationDao.findAll();
	}

	@Override
	public MerchantApplication get(String id) {
		return merchantApplicationDao.load(id);
	}

	@Override
	public List<MerchantApplication> getByStatus(
			String status) {
		if (status.equals("success")) {
			List<MerchantApplication> applications = merchantApplicationDao.findByStatus(MerchantApplication.BLACK_LIST);
			applications.addAll(merchantApplicationDao.findByStatus(MerchantApplication.WHITE_LIST));
			return applications;
		}
		return merchantApplicationDao.findByStatus(status);
	}

	@Override
	public MerchantApplication getByName(String name) {
		return merchantApplicationDao.loadByName(name);
	}

	@Override
	public String updateStatusByName(String name,String operation) {
		
		String result = "SUCCESS";
		MerchantApplication mApplication =merchantApplicationDao.loadByName(name);
		String bstatus = mApplication.getStatus();
		int m = 0;
		if(MerchantApplication.BLACK_LIST.equals(bstatus)&&operation.equals(MerchantApplication.WHITE_LIST)){
			m = merchantApplicationDao.updateStatusByName(name, MerchantApplication.WHITE_LIST);
			versionTable.put(VersionTable.STATUS_CHANGE__VERSION, System.currentTimeMillis());
			return result;
		}
		if(MerchantApplication.WHITE_LIST.equals(bstatus)&&operation.equals(MerchantApplication.BLACK_LIST)){
			m = merchantApplicationDao.updateStatusByName(name, MerchantApplication.BLACK_LIST);
			versionTable.put(VersionTable.STATUS_CHANGE__VERSION, System.currentTimeMillis());
			return result;
		}
		return "illegal operation";
	}

	@Override
	public int updateByName(MerchantApplication application) {
		return merchantApplicationDao.updateByName(application);
	}

	@Override
	public String audit(MerchantApplication application) {
		MerchantApplication daApplication = merchantApplicationDao.loadByName(application.getName());
		if (daApplication.getUpdateTime()!=null&&daApplication.getUpdateTime().equals(application.getUpdateTime())) {
			if (MerchantApplication.WAIT_AUDIT.equals(daApplication.getStatus()) && MerchantApplication.WHITE_LIST.equals(application.getStatus())) {
				int m = merchantApplicationDao.updateStatusByName(application.getName(), MerchantApplication.WHITE_LIST);
				versionTable.put(VersionTable.AUDIT_VERSION, System.currentTimeMillis());
				return "success";
			}
			if( MerchantApplication.REJECT.equals(application.getStatus())){
				if (!MerchantApplication.WAIT_AUDIT.equals(daApplication.getStatus())) {
					return "illegal change";
				}
				if("".equals(application.getRejectReason())){
					return "reson can not empty";
				}
				daApplication.setStatus(application.getStatus());
				daApplication.setRejectReason(application.getRejectReason());
				int m = merchantApplicationDao.updateByName(daApplication);
				versionTable.put(VersionTable.AUDIT_VERSION, System.currentTimeMillis());
				return "success";
			}
			return "operation not allow";
		}
		return "data is update";
		
	}




}
